import { BaseController } from './base';
import jwt from 'jsonwebtoken';
import sequelize from '../config/db';
import { JWT_SECRECT, expireTime } from '../config/jwt';
import Crypto from 'crypto';
import { Context } from 'koa';


export class UserController {
  static login = async (ctx: Context) => {
    const { username, password } = ctx.request.body;
    console.log(ctx.request.body, username, 'ctx.body')

    const user = await sequelize.query(`SELECT * from blade_user where account = ${username}`);
    const encodePassword = Crypto.createHash('md5');
    encodePassword.update(password);
    const str = encodePassword.digest();
    console.log(str, 'password====>');
    const time =  Math.floor(Date.now() / 1000) + expireTime;
    const token = jwt.sign({
      data: user,
      exp: time
    }, JWT_SECRECT)
    ctx.body = token;
  }

  static register = async (ctx: Context) => {
    
  }
}